<?php
ob_start();
session_start();
require_once 'includes/image_resizer.php'; 
//print_r($_REQUEST);
$smsg="";
//if($_SESSION['user_id_sess']=="") {
/*	echo "<script type='text/javascript'> window.location='index.php?signin&url=post'; </script>";*/
//}
@extract($_REQUEST);
$userId=$_SESSION['user_id_sess'];
if(isset($_REQUEST['keyword']))
{

	$today=date("Y-m-d G:i:s",time());
	
	$duration=$_REQUEST['post_duration'];
	$bargainDuration = date("Y-m-d G:i:s",time() + ($duration * 24 * 60 * 60));		
	$post_name=$_REQUEST['post_name'];	
	$post_location=$_REQUEST['post_location'];
	$bedroom=$_REQUEST['bedrooms'];
	$bathroom=$_REQUEST['bathrooms'];
	$price=$_REQUEST['price'];
	
	$category=$_REQUEST['post_category'];	
	$post_description=$_REQUEST['post_description'];
	$post_duration=$_REQUEST['post_duration'];	
	$remarks=$_REQUEST['post_remarks'];
	
	
	if($_REQUEST['post_category']=='2'){
	$sqlPost="insert into tbl_post set post_name='$post_name',
				category_id='$category',
				subcategory_id='$pro_subcategory',				
				post_description='$post_description',
				post_location='$post_location',
				price   = '$jobprice',
				post_company='$compName',
				post_contact='$contact',
				post_phone='$phone',
				post_email='$email',
				post_experience='$experience',
				education='$education',
				post_remarks='$remarks',
				post_duration='$post_duration',			
				posted_by='$userId',
				posted_date='$today'";
		$rstPost=mysql_query($sqlPost) or die(mysql_error());		
	}
	
	if($_REQUEST['post_category']=='3'){
	$sqlPost="insert into tbl_post set post_name='$post_name',
				category_id='$category',
				subcategory_id='$pro_subcategory',				
				post_description='$post_description',
				post_location='$post_location',
				price   = '$rentprice',
				post_company='$company',
				bedroom='$bedrooms',
				bathroom='$bathrooms',
				post_sqrt='$sqrtfeet',
				post_yearbuild='$yearbuild',
				post_property='$property',
				post_remarks='$remarks',
				post_duration='$post_duration',			
				posted_by='$userId',
				posted_date='$today'";
		$rstPost=mysql_query($sqlPost) or die(mysql_error());		
	}		
	if($_REQUEST['post_category']=='4'){
	$sqlPost="insert into tbl_post set post_name='$post_name',
				category_id='$category',
				subcategory_id='$pro_subcategory',				
				post_description='$post_description',
				post_location='$post_location',
				price   = '$realprice',
				post_company='$company',
				bedroom='$bedrooms',
				bathroom='$bathrooms',
				post_sqrt='$sqrtfeet',
				post_yearbuild='$yearbuild',
				post_property='$property',
				post_remarks='$remarks',
				post_duration='$post_duration',			
				posted_by='$userId',
				posted_date='$today'";
		$rstPost=mysql_query($sqlPost) or die(mysql_error());		
	}
	if($_REQUEST['post_category']=='5'){
	$sqlPost="insert into tbl_post set post_name='$post_name',
				category_id='$category',
				subcategory_id='$pro_subcategory',				
				post_description='$post_description',
				post_location='$post_location',
				price   = '$serviceprice',
				post_company='$company',
				post_remarks='$remarks',
				post_duration='$post_duration',			
				posted_by='$userId',
				posted_date='$today'";
		$rstPost=mysql_query($sqlPost) or die(mysql_error());		
	}
	
	if(mysql_affected_rows()>0) {	
		$smsg="true";
		$postId=mysql_insert_id();	
		$postImage=pathinfo($_FILES["post_image"]["name"]);		
			if(!empty($postImage['extension'])) {			
				if(strtolower($postImage['extension'])=="jpeg" || strtolower($postImage['extension'])=="jpg" || 
					strtolower($postImage['extension'])=="png" || strtolower($postImage['extension'])=="gif") {											
					$orgname=mt_rand();				
					$type=$postImage['extension'];				
					$path="posts/original/".$orgname.$postId.".".$type;					
					$path_thumb="posts/thumb/".$orgname.$postId."_thumb.".$type;					
					$path_medium="posts/medium/".$orgname.$postId."_medium.".$type;									
					move_uploaded_file($_FILES["post_image"]["tmp_name"],$path);				  
					productResize($path, $path_thumb, $path_medium, $type);	
					$imagePath[] = 	$path;	
					$mediumPath[] = $path_medium;	
					$thumbPath[] = 	$path_thumb;	
					$sql_updpro="UPDATE tbl_post set 
								 post_image_thumb='".$path_thumb."',
								 post_image_sample='".$path_medium."',
								 post_image='".$path."'
								 where post_id=$postId";	
					$rst_updpro=mysql_query($sql_updpro) or die(mysql_error());
					if(mysql_affected_rows()==0) { $smsg="false"; }	
				}
			 }		
	}
	else { $smsg="false"; }	
}


?>
<style type="text/css">
	#sell-form { border:0px solid #CCC; vertical-align:top; padding:10px; float:left; width:93%; margin:2%; }

	#sell-form label{
	float: left;
	width: 130px;
	font-weight: bold;
	}
	
	#sell-form input, select, textarea{	
	width: 280px;
	margin-bottom: 5px;
	font-size:12px;
	}
	
	#sell-form textarea{
	height: 250px;
	width: 450px;
	font-size:12px
	}
	
	#sell-form #submitbutton{
	margin-left: 130px;
	margin-top: 5px;
	width: 90px;
	}
	
	#sell-form br{
	clear: left;
	}

	
</style>
<div id='panel'>

<div class='mypg_left_out'>
<? include("mypg_left.php"); ?>
</div>


<div class='fullbox_out'>
<?php
if($_SESSION['user_type_sess']==3)
{
   include_once "my_swapen_merchant.php";
}
else if($_SESSION['user_type_sess']==1 || $_SESSION['user_type_sess']==2 || $_SESSION['user_type_sess']==4)
{
  include_once "my_swapen_seller.php";
}
else
{
     include("my_swapen_wholesaler.php");
}

?>
	<a name="pst" id="pst"></a>
	
	<div class="fulldiv" style="margin:10px 0px 0px 0px;">
	
	<p class="pro_ptag">
	<strong class="orangehead" style="margin:0px 0px 0px 7px;">Posts</strong>
	<a href="index.php?amazon_email" style="float:right; margin:0px 10px 0px 0px;">Amazon Payment Account</a>
	</p>
	
	<div class="fulldiv">
		<div class="fullbox_top">&nbsp;</div>
		<div class="fullbox_repeat">
		<table width="100%" border="0">
	
	  <tr>
		<td colspan="2">
		
		<table width="100%" align="center"  border="0" cellspacing="0">
		  <tr>
			<td><?php if($smsg=="true") { echo "<div id='suc-msg'> Posts inserted successfully. </div>"; } ?></td>
		  </tr>
		  <tr>
			<td height="500">		
			<form name="frmPost" id="frmPost" method="post" action="" enctype="multipart/form-data">    
				  <input type="hidden" name="keyword" id="keyword">
					<div id="sell-form">
						
						<p class="pro_ptag">				
						<label for="post_category">Category</label>
						<select name="post_category" id="post_category" onChange="javascript:funGetFields(this.value, 'get-fields'); getSubcat(this.value,'sub-category');" class="inputbg">
							<option value="">..Select Category..</option>
							<?php				
							 $sql_shocat="SELECT * FROM tbl_category where parent_id='0' LIMIT 1,5";
							 $rst_shocat=mysql_query($sql_shocat);
							 while($row_shocat=mysql_fetch_array($rst_shocat)) {
								 $cat_id=$row_shocat['category_id'];
								 $cat_name=$row_shocat['category_name'];							 
								 echo "<option value='$cat_id'>$cat_name</option>";
							 }
							?>
						</select>
						</p>
						<span id="loader-div"></span>
										
						<span id="sub-category"> </span>
						
						<p class="pro_ptag">
						<label for="post_name">Post Name</label>
						<input type="text" name="post_name" id="post_name" style="width:450px;" class="inputbg"><br>
						</p>
						
						<p class="pro_ptag">
						<label for="post_description">Description</label>
						<textarea name="post_description" id="post_description" class="inputbg"></textarea><br>					
						</p>
						
						<p class="pro_ptag">
						<label for="post_location">Location</label>
						<input type="text" name="post_location" id="post_location" class="inputbg">
						</p>
	
						<span id="get-fields"> </span>

						<p class="pro_ptag">
						<label for="post_image">Post Image</label>
						<input type="file" name="post_image" id="post_image" class="inputbg">
						</p>
						
						<!--<label for="post_video">Post Video</label>
						<input type="file" name="post_video" id="post_video" disabled><br> -->
						
						<p class="pro_ptag">										
						<label for="post_duration">Duration</label>
						<select name="post_duration" id="post_duration" style="width:84px;" class="inputbg">										
							<option value='3'>3 days</option>
							<option value='5'>5 days</option>
							<option value='7' selected>7 days</option>
							<option value='10'>10 days</option>						
						  </select>					
						</p>			
						
						<p class="pro_ptag">
						<label for="post_remarks">Remarks</label>
						<textarea name="post_remarks" id="post_remarks" style="height:40px; font-size:12px;" class="inputbg"></textarea><br>
						<?php
						if($_SESSION['user_id_sess']=="") {
						?>
						<input type="button" onClick="show_login()" id="submitbutton" name="submitbutton" value="Submit" class="yellow_btn"><br>
						<?php
						}
						else {?>
						<input type="button" onClick="funPost();" id="submitbutton" name="submitbutton" value="Submit" class="yellow_btn"><br>
						<?
						} ?>
					</p></div><br>
			</form></td>
		  </tr>
		</table></td>
	  </tr>
	</table>
		</div>
		<div class="fullbox_bot">&nbsp;</div>
	</div>
	
	</div>
</div>

</div>

<div class="user_popup" id="login_box">
  <div class="full_div">
  <span style="float: right; padding: 5px; font-weight: bold; cursor: pointer;" onclick="$('#login_box').fadeOut('slow');">x</span>
  </div>

  <div class="full_div" align="center">
    <div class="input_left">&nbsp;</div>
    <input type="text" name="username" value="Username" id="username" class="input_repeat" style="width:130px;" onBlur="putIndicator(this,'Username')" onFocus="clearIndicator(this,'Username')" onClick="clearIndicator(this,'Username')" />
    <div class="input_right">&nbsp;</div>
  </div>
  <div class="full_div" align="center">
    <div class="input_left">&nbsp;</div>
    <input type="password" name="password" value="Password" id="password" class="input_repeat" style="width:130px;"  onBlur="putIndicator(this,'Password')" onFocus="clearIndicator(this,'Password')" onClick="clearIndicator(this,'Password')" />
    <div class="input_right">&nbsp;</div>
  </div>
  <div class="fulldiv" align="center" >
     <input type="button" name="login" value="Sign In " id="login" class="button" onclick="validateLogin()" />
     <span style="font-weight:normal;">If you are not a member yet please <a href="<?php echo $base_url;?>index.php?signup">register</a> first.</span>
     <span style="color:red;" id="login_error"></span>
   </div>
  
</div>


<script>
function show_login() {
	var pos = $('#submitbutton').position();
	var nleft = parseInt(pos.left)+230;
	var ntop = parseInt(pos.top)-300;
	$('#login_box').css({'left': nleft, 'top':ntop});
	$('#login_box').fadeIn('slow');
}
function validateLogin()
  {
   // alert('hi');
    var username = $('#username').val();
    var password = $('#password').val();
    $('#login_error').html('');
    if(username=='' || password=='')
    {
      alert('Please enter username and password');
    }
    else
    {
      //alert('hi');
      var url = "<?php echo $base_url;?>ajax_signin.php";
      $.post(url,
      { 'username': username, 'password':password},
      function(data)
      {
	//alert(data);
	if(data=='Your account is on hold, please contact us for more information')
	  $('#login_error').html(data);
	else if(data=='Your Account has been placed on the inactive list, please contact Swapen Support')
	  $('#login_error').html(data);
	else if(data=='Invalid user name and password.')
	  $('#login_error').html(data);
	else if(data=='')
	  window.location='index.php';
	else
	  window.location='index.php?'+data;
	      
      },
      'html'
      );
    }
  }	
	
</script>